/*
 * Copyright 2008 Wink Saville
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.saville.android.testnet1;

import java.io.*;
import java.net.*;
//import java.nio.*;
import java.nio.channels.SocketChannel;
import android.os.Bundle;
import com.saville.debug.Log;
import com.saville.msgcomp.*;
import com.saville.mc.*;

public class TestNet1 extends McActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle, "TestNet1");
        setContentView(R.layout.main);
        
        webGet("192.168.0.133", 80);
        nioWebGet("192.168.0.133", 80);
    }
    
    public void processMsg(Msg msg) {
    }
    
    private void webGet(String addr, int port) {
        Socket socket = null;
        DataOutputStream outStream = null;
        InputStreamReader inStream = null;
        
        try {
            socket = new Socket(addr, port);
            outStream = new DataOutputStream(socket.getOutputStream());
            inStream = new InputStreamReader(socket.getInputStream());
            BufferedReader r = new BufferedReader(inStream);
            
            outStream.writeBytes("GET / HTTP/1.0\n\r\n\r");
            
            while(true) {
                String data = r.readLine();
                if (data == null)
                    break;
                Log.print(data);
            }
        } catch (IOException e) {
            Log.e(df, "webGet: error e=%s", e);
        } finally {
            try {
                if (outStream != null)
                    outStream.close();
                if (inStream != null)
                    inStream.close();
                if (socket != null)
                    socket.close();
            } catch (IOException e) {
                Log.e(df, "webGet: error closing inStream e=%s", e);
            }
        }
    }
    
    private void nioWebGet(String addr, int port) {
        SocketChannel socket = null;
        
        try {
            /*
             * Not implemented yet SocketChannel.open() fails
             */
            socket = SocketChannel.open(new InetSocketAddress(addr, port));
        } catch (Exception e) {
            Log.e(df, "nioWebGet: error e=%s", e);
        } finally {
            try {
                if (socket != null) {
                    socket.close();
                }
            } catch (IOException e) {
            }
        }
    }
    
    public int df = Log.VERBOSE;
}